package com.zjc.yohoshop.mvp.view.fragment;

import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.zjc.lib_core.base.BaseFragment;
import com.zjc.yohoshop.R;
import com.zjc.yohoshop.bean.ShoesEntity;
import com.zjc.yohoshop.di.compont.DaggerShoesFragmentCompont;
import com.zjc.yohoshop.di.modules.ShoesFragmentModules;
import com.zjc.yohoshop.mvp.contract.ShoesFragmentContract;
import com.zjc.yohoshop.mvp.persenter.ShoesFragmentPresenter;
import com.zjc.yohoshop.mvp.view.adapter.ShoesAdapter;
import com.zjc.yohoshop.mvp.view.adapter.ShoesBannerViewPagerAdapter;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 球鞋交易
 */
public class ShoesFragment extends BaseFragment<ShoesFragmentPresenter> implements ShoesFragmentContract.ShoesFragmentIView, BaseRefreshListener, View.OnClickListener {

    private LinearLayout ll_shoes_search;
    private PullToRefreshLayout ptl_shoes;
    private RecyclerView rv_shoes;
    private ShoesAdapter shoesAdapter;
    private ViewPager vp__item_shoes_banner;
    private ImageView iv_shoes_nfc, iv_shoes_all;
    private int num = 0;
    private boolean zf = false;
    private Timer timer;


    @Override
    public View createView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_shoes, null);
        return view;
    }

    @Override
    public void init(View view) {
        ll_shoes_search = view.findViewById(R.id.ll_shoes_search);
        ptl_shoes = view.findViewById(R.id.ptl_shoes);
        iv_shoes_nfc = view.findViewById(R.id.iv_shoes_nfc);
        iv_shoes_all = view.findViewById(R.id.iv_shoes_all);
        //球鞋交易顶部滑动广告控件初始化
        View view_banner = View.inflate(getActivity(), R.layout.item_shoes_banner, null);
        vp__item_shoes_banner = view_banner.findViewById(R.id.vp__item_shoes_banner);
        vp__item_shoes_banner.setOffscreenPageLimit(3);//一页显示3条
        vp__item_shoes_banner.setPageMargin(20);//每页外边距
        //球鞋交易brand部分控件初始话
        View view_brand = View.inflate(getActivity(), R.layout.item_shoes_brand, null);

        //球鞋交易下方列表空间初始化
        //适配器配置
        rv_shoes = view.findViewById(R.id.rv_shoes);
        rv_shoes.setLayoutManager(new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false));
        shoesAdapter = new ShoesAdapter(R.layout.item_shoes_values);
        //适配器中添加头布局
        shoesAdapter.addHeaderView(view_banner,0);
        shoesAdapter.addHeaderView(view_brand, 1);
        rv_shoes.setAdapter(shoesAdapter);
    }

    @Override
    public void initData() {
        p.request();
    }

    @Override
    public void setupDagger() {
        DaggerShoesFragmentCompont.builder().shoesFragmentModules(new ShoesFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void initListener() {
        ptl_shoes.setRefreshListener(this);
        ll_shoes_search.setOnClickListener(this);
        //头部ViewPager滚动事件
        vp__item_shoes_banner.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
                //滚动页
                num = i;

            }

            @Override
            public void onPageSelected(int i) {
                //页选择
            }

            @Override
            public void onPageScrollStateChanged(int i) {
                //在页面上滚动状态改变
            }
        });
        //头部ViewPager点击事件
        vp__item_shoes_banner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "头部滚动图", Toast.LENGTH_SHORT).show();
            }
        });

        //商品点击事件
        shoesAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(getActivity(), ((ShoesAdapter)shoesAdapter).getData().get(position).goods_name, Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void success(ShoesEntity entity) {
        if (entity.values == null || entity.values.size() == 0 || entity.banner == null || entity.banner.size() == 0 || entity.brand == null || entity.brand.size() == 0){
            Toast.makeText(getActivity(), "没数据了", Toast.LENGTH_SHORT).show();
        }
        shoesAdapter.addData(entity.values);

        vp__item_shoes_banner.setAdapter(new ShoesBannerViewPagerAdapter(entity.banner));
        startTimer(entity.banner.size() - 1);
//        vp__item_shoes_banner.setCurrentItem(entity.banner.size() / 2);
    }

    private void startTimer(final int end){
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (num >= end){
                    zf = !zf;
                }
                if (num <= 0){
                    zf = !zf;
                }
                if (zf){
                    num++;
                }else{
                    num--;
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        vp__item_shoes_banner.setCurrentItem(num);
                    }
                });
            }
        },  0, 1500);
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }


    @Override
    public void refresh() {
        shoesAdapter.getData().clear();
        p.refresh();
        ptl_shoes.finishRefresh();
    }

    @Override
    public void loadMore() {
        p.loadMore();
        ptl_shoes.finishLoadMore();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_shoes_search:
                Toast.makeText(getActivity(), "搜索框", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_shoes_nfc:
                Toast.makeText(getActivity(), "nfc", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_shoes_all:
                Toast.makeText(getActivity(), "all", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
}
